home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 02 - Basic Game Graphics / HSV Demo 1 ƒ / HSV Demo 1.c next >
Encoding:
C/C++ Source or Header  |  1995-03-30  |  6.2 KB  |  216 lines  |  [TEXT/MMCC]

  1. //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  2. //
  3. //    HSV Demo 1.c
  4. //
  5. //    Demonstrates drawing with HSV colors. Uses a default System
  6. //    Palette (note the banding which occurs due to default color 
  7. //    distribution.)
  8. //
  9. //    History:
  10. //
  11. //    950225 jb: Written
  12. //
  13. //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  14.  
  15.  
  16. //  __#Defines________________________________________________________________________
  17. #define kMainWindowResID            1000
  18. #define kAppColorTableResID        72                    //ID of nice, 256-color palette provided by System
  19. #define    kSleepTicks                        0xFFFFFFFF    //relinquish all time; don't want null events
  20.  
  21. //  __#Headers________________________________________________________________________
  22. #include <Palettes.h>
  23. #include <math.h>
  24. #include "Utils.h"    //for EnviroCheck
  25.  
  26. //  __#Protos_________________________________________________________________________
  27. //  __ Macros_________________________________________________________________________
  28. //  __ Enums__________________________________________________________________________
  29. //  __ Typedefs_______________________________________________________________________
  30. //  __ Static Protos__________________________________________________________________
  31. static void ToolBoxInit(void);
  32. static Boolean OpenMainWindow( void );
  33. static void ShowHSVCircle( void );
  34. static void WaitForQuit( void );
  35.  
  36. //  __ Extern Globals_________________________________________________________________
  37. //  __ Static Globals_________________________________________________________________
  38. static WindowPtr                gMainWindow;
  39.  
  40. //  __ Functions______________________________________________________________________
  41.  
  42.  
  43.  
  44.  
  45.  
  46. //____ main __________________________________________________________________________
  47. //
  48. void main( void )
  49. {
  50.  
  51.     ToolBoxInit();
  52.     
  53.     if (!EnviroCheck())
  54.         ExitToShell();
  55.  
  56.     if (!OpenMainWindow())
  57.         ExitToShell();
  58.     
  59.     ShowHSVCircle();
  60.     
  61.     WaitForQuit();
  62.     
  63.     if(NULL != gMainWindow)
  64.         DisposeWindow(gMainWindow);
  65.             
  66. }//main
  67.  
  68.  
  69.  
  70. //____ ToolBoxInit __________________________________________________________________________
  71. //
  72. //    Basic initialization.
  73. //
  74. static void ToolBoxInit( void )
  75. {    
  76.     InitGraf(&qd.thePort);
  77.     InitFonts();
  78.     InitWindows();
  79.     InitMenus();
  80.     TEInit();
  81.     InitDialogs(NULL);
  82.     InitCursor();
  83.     FlushEvents(everyEvent, 0);
  84. }    //ToolBoxInit
  85.  
  86.  
  87. //____ OpenMainWindow __________________________________________________________________________
  88. //
  89. //    Open our window, install palette built from a resource CTable.
  90. //    provided by the System.
  91. //
  92. //    All entries in palette marked pmTolerant, with a tolerance of 0x5000.
  93. //
  94. static Boolean OpenMainWindow( void )
  95. {
  96. PaletteHandle appPalette;                // the palette that we will make the window's palette
  97. CTabHandle            tempColorTable;
  98.  
  99.  
  100.     // create and show the window, make sure it's frontmost
  101.     gMainWindow = GetNewCWindow( kMainWindowResID, ( Ptr )NULL, ( WindowPtr ) -1 );    
  102.     if (NULL == gMainWindow)            //real apps should handle this gracefully
  103.         return FALSE;
  104.     SetWTitle(gMainWindow, "\pHSV Demo 1");
  105.     ShowWindow( gMainWindow );
  106.     SetPort( gMainWindow );
  107.  
  108.     //load in the color table, translate to palette, install in window
  109.     tempColorTable = GetCTable( kAppColorTableResID );
  110.     if (NULL == tempColorTable)
  111.         return FALSE;
  112.  
  113.     appPalette = NewPalette( 256, tempColorTable, pmTolerant, 0x5000 );
  114.     NSetPalette( gMainWindow, appPalette, pmFgUpdates );
  115.     
  116.     DisposeCTable(tempColorTable);
  117.     
  118.     return TRUE;
  119. }//OpenMainWindow
  120.  
  121.  
  122. //____ ShowHSVCircle __________________________________________________________________________
  123. //
  124. //    Walk incrementally around the perimeter of the HSV color wheel, creating HSV colors
  125. //    on each step, translating them to RGB, and then drawing a dot with that color
  126. //    on a circle corresponding the color's origin on the HSV wheel.
  127. //
  128. //    Changing the value of kNumCircles to a number greater than one will cause more HSV
  129. //    circles to be drawn, each of which will exhibit increasing Saturation and Value.
  130. //
  131. static void ShowHSVCircle( void )
  132. {
  133. #define        kNumSteps                    256                                                //defines how many dots
  134. #define        mThetaStepSize         (6.283 / kNumSteps)                //how far around our circle to go
  135. #define        mColorStepSize        (65536 / kNumSteps)                //how far to go around color wheel per dot
  136. #define        kNumCircles                1                                                    // <== try changing this to 5 or 10
  137. #define        kRadius                        120                                                //max radius of our circle
  138. #define        mRadiusStep                (kRadius / kNumCircles)        //how much to enlarge each circle
  139. #define        mSatValStep                (65535 / kNumCircles)            //amount to increments saturation and value
  140. #define        kHalfRectSize            20                                                //halve length to make centering easy
  141.  
  142. short        step;
  143. short        x, y, cenX, cenY;
  144. double    theta;
  145. Rect        aRect;
  146. short        theRadius;
  147. short        circleNum;
  148. HSVColor    ourHSV;
  149. RGBColor    resultingRGB;
  150.  
  151.     cenX = qd.thePort->portRect.right / 2;
  152.     cenY = qd.thePort->portRect.bottom / 2;
  153.     
  154.     resultingRGB.red = resultingRGB.blue = resultingRGB.green = 32768;
  155.     RGBForeColor(&resultingRGB);
  156.     PaintRect(&qd.thePort->portRect);
  157.     
  158.     //start out with full red
  159.     ourHSV.hue = 0;
  160.     ourHSV.saturation = 0;
  161.     ourHSV.value = 0;
  162.     theRadius = 0;
  163.     for (circleNum = 0; circleNum < kNumCircles; circleNum++)
  164.     {        
  165.         theRadius += mRadiusStep; 
  166.         ourHSV.saturation += mSatValStep;
  167.         ourHSV.value += mSatValStep;
  168.  
  169.         //draw HSV circle
  170.         for (step = 0, theta = 0.0; step < kNumSteps; step++)
  171.         {
  172.             theta += mThetaStepSize;
  173.             x = cenX + theRadius * cos(theta);
  174.             y = cenY - theRadius * sin(theta);
  175.     
  176.             ourHSV.hue += mColorStepSize;                //more clockwise along color wheel
  177.             HSV2RGB(&ourHSV,&resultingRGB);
  178.     
  179.             RGBForeColor(&resultingRGB);
  180.             
  181.             SetRect(&aRect, x - kHalfRectSize, y - kHalfRectSize, x + kHalfRectSize, y + kHalfRectSize);
  182.             PaintOval(&aRect);
  183.         }
  184.  
  185.     }//for circleNum
  186. }//ShowHSVCircle
  187.  
  188.  
  189. //____ WaitForQuit __________________________________________________________________________
  190. //
  191. //    WaitForQuit -
  192. //
  193. //    Hangs out until there is any keyboard or mouse activity. Not a very useful event loop,
  194. //    but sufficient for our demo.
  195. //
  196. static void WaitForQuit( void )
  197. {
  198. Boolean    wait;
  199. EventRecord        theEvent;
  200.     
  201.     wait = TRUE;
  202.     while (wait)
  203.     {
  204.         if(WaitNextEvent(everyEvent, &theEvent, kSleepTicks, 0L))
  205.         {
  206.             switch (theEvent.what)
  207.             {
  208.                 case mouseDown:
  209.                 case keyDown:
  210.                     wait = FALSE;
  211.                 break;
  212.             }//switch (theEvent.what)
  213.         }//if WaitNextEvent
  214.     }//while
  215.  
  216. }//WaitForQuit